package com.lonely.fx;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @date: 2022-06-23 17:43
 * @author: ztkj-hzb
 * @description: group组件的设置
 **/
public class GroupSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        Button button1 = new Button("加按钮");
        button1.setLayoutX(0);
        button1.setLayoutY(10);

        Button button2 = new Button("减按钮");
        button2.setLayoutX(100);
        button2.setLayoutY(10);

        //将node添加到group中
        Group group = new Group();
        //group.getChildren().add(button1);
        //group.getChildren().add(button2);
        group.getChildren().addAll(button1, button2);

        //添加节点变化事件
        group.getChildren().addListener((ListChangeListener<Node>) c -> {
            System.out.println("当前group组件中的按钮数量：" + c.getList().size());
        });

        AtomicInteger size = new AtomicInteger(0);
        button1.setOnAction(event -> {
            Button button = new Button("新按钮");
            button.setLayoutX(50 * size.get());
            button.setLayoutY(50);
            size.incrementAndGet();
            group.getChildren().add(button);
        });
        button2.setOnAction(event -> {
            group.getChildren().remove(group.getChildren().size() - 1);
            size.decrementAndGet();
        });


        Scene scene = new Scene(group);
        primaryStage.setScene(scene);

        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.show();


    }
}
